package code.c03_stackQueue.cSqQueue;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/8 19:47
 * @package code.c03_stackQueue.cSqQueue
 * @description 对于循环队列来所，如果知道队头指针和队中元素个数，则可以计算出队尾指针，也就是说可以用队中元素的个数代替队尾指针。
 * 设计一出这种循环队列 判队空 进队 出队 和 取队头元素的算法
 */

public class Exam_3_12 {

}

class CSqQueueClass1<E>{
    final int MAX_SIZE = 100;
    private E[] data;
    private int front;
    private int count;

    public CSqQueueClass1() {
        data = (E[])new Object[MAX_SIZE];
        front = 0;
        count = 0;
    }


    public boolean empty(){
        return count == 0;
    }

    public void push(E e){
        int rear;
        rear = (front + count) %MAX_SIZE;
        if (count == MAX_SIZE){
            throw new IllegalArgumentException("队满");
        }

        rear = (rear +1) %MAX_SIZE;
        data[rear] = e;
        count ++;
    }

    public E pop(){
        if (empty()){
            throw new IllegalArgumentException("对空");
        }

        front = (front +1)%MAX_SIZE;
        count --;
        return data[front];
    }

    public E peek(){
        if (empty()){
            throw new IllegalArgumentException("队空");
        }

        return data[(front +1)%MAX_SIZE];
    }
}
